package com.up.upzl.biz.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.up.upzl.base.service.impl.BaseServiceImpl;
import com.up.upzl.biz.domain.PaiDianRecord;
import com.up.upzl.biz.domain.PaiDianRecord.HuanXian;
import com.up.upzl.biz.domain.PaiDianRecord.JiangjinStatus;
import com.up.upzl.biz.repository.PaiDianRecordRepository;
import com.up.upzl.biz.service.PaiDianRecordService;

import utils.UpzlUtils;

@Service
public class PaiDianRecordServiceImpl extends BaseServiceImpl<PaiDianRecord, Long> implements PaiDianRecordService {

	@Autowired
	private PaiDianRecordRepository paiDianRecordRepository;

	@Override
	public Page<PaiDianRecord> findByCondition(Integer areaId, Integer businessAreaId, HuanXian huanXian, String phone,
			String wechatId, Date createDateBegin, Date createDateEnd, PaiDianRecord.Status status, Pageable page,
			String huanxian1) {
		// Sort sort = new Sort(Direction.DESC, "id");
		Page<PaiDianRecord> nPage = paiDianRecordRepository.findAll(new Specification<PaiDianRecord>() {
			/**
			 * root：获得需要查询的属性 query：可以排序 cb：构造查询条件，包含的构造条件有很多的，详细的可以查看API
			 */
			@Override
			public Predicate toPredicate(Root<PaiDianRecord> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> predicates = new ArrayList<>();
				if (areaId != null) {
					predicates.add(cb.and(cb.equal(root.get("area").get("id"), areaId)));
				}
				if (businessAreaId != null) {
					predicates.add(cb.and(cb.equal(root.get("businessArea").get("id"), businessAreaId)));
				}
				if (huanXian != null) {
					predicates.add(cb.and(cb.equal(root.get("huanXian"), huanXian)));
				}
				if (huanxian1 != null) {
					predicates.add(cb.and(cb.equal(root.get("huanxian1"), huanxian1)));
				}
				if (phone != null) {
					predicates.add(cb.like(root.get("phone"), "%" + phone + "%"));
				}
				if (wechatId != null) {
					predicates.add(cb.equal(root.get("wechatId"), wechatId));
				}
				if (status != null) {
					predicates.add(cb.equal(root.get("status"), status));
				}
				if (createDateBegin != null) {
					predicates.add(cb.and(
							cb.greaterThanOrEqualTo(root.get("updateTime"), UpzlUtils.timeHelper(createDateBegin))));
				}
				if (createDateEnd != null) {
					predicates.add(
							cb.and(cb.lessThanOrEqualTo(root.get("updateTime"), UpzlUtils.timeHelper(createDateEnd))));
				}
				query.orderBy(cb.desc(root.get("createTime").as(Date.class)));
				return cb.and(predicates.toArray(new Predicate[predicates.size()]));
				// return query.getRestriction();
			}
		}, page);
		return nPage;
	}

	@Override
	public Page<PaiDianRecord> findByCondition(Integer area, Integer businessArea, HuanXian huanXian, String phone,
			String wechatId, Date dateBegin, Date dateEnd, JiangjinStatus status, Pageable page) {
		return paiDianRecordRepository.findAll(new Specification<PaiDianRecord>() {

			@Override
			public Predicate toPredicate(Root<PaiDianRecord> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> predicates = new ArrayList<>();
				if (area != null) {
					predicates.add(cb.and(cb.equal(root.get("area").get("id"), area)));
				}
				if (businessArea != null) {
					predicates.add(cb.and(cb.equal(root.get("businessArea").get("id"), businessArea)));
				}
				if (huanXian != null) {
					predicates.add(cb.and(cb.equal(root.get("huanXian"), huanXian)));
				}
				if (phone != null) {
					predicates.add(cb.like(root.get("phone"), "%" + phone + "%"));
				}
				if (wechatId != null) {
					predicates.add(cb.equal(root.get("wechatId"), wechatId));
				}
				if (status != null) {
					predicates.add(cb.equal(root.get("jiangjinStatus"), status));
				}
				if (dateBegin != null) {
					predicates.add(cb.and(cb.greaterThanOrEqualTo(root.get("updateTime"), dateBegin)));
				}
				if (dateEnd != null) {
					predicates.add(cb.and(cb.lessThanOrEqualTo(root.get("updateTime"), UpzlUtils.timeHelper(dateEnd))));
				}
				query.orderBy(cb.desc(root.get("updateTime").as(Date.class)));
				return cb.and(predicates.toArray(new Predicate[predicates.size()]));
			}
		}, page);
	}

	@Override
	public Long countByJiangjinStatus(JiangjinStatus status) {
		return paiDianRecordRepository.countByJiangjinStatus(status);
	}

	@Override
	public List<PaiDianRecord> findByPhone(String phone) {
		return paiDianRecordRepository.findByPhone(phone);

	}

}
